package com.example.mardan_demo1.controller;

import com.example.mardan_demo1.pojo.Announcement.Announcement;
import com.example.mardan_demo1.pojo.FilePojo;
import com.example.mardan_demo1.pojo.PageBean;
import com.example.mardan_demo1.pojo.Result;
import com.example.mardan_demo1.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.UUID;

@RestController
public class FileUploadController {

    @Autowired
    FileUploadService fileUploadService;

    @PostMapping("/upload/list")
    public Result<PageBean<FilePojo>> list (Integer pageNum,
                                            Integer pageSize,
                                            String name) {
        PageBean<FilePojo> pb = fileUploadService.list(pageNum, pageSize, name);
        return Result.success(pb);
    }
    /**
     *图片上传
     */
    @PostMapping("/upload-image")
    public Result uploadImage(MultipartFile file)  throws IOException, URISyntaxException {
        return fileUploadService.uploadImg(file);
    }

    /**
     * 文件上传
     * ApiDes:
     * @param file
     * @return 响应描述：
     */

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException, URISyntaxException {
        String UPLOAD_DIR = "/home/oracle/uploadfiles";
        if (System.getProperty("os.name").toLowerCase().contains("windows")) {
//            UPLOAD_DIR = "C:\\Users\\node\\Downloads\\shujutongji\\mardan-demo1\\src\\main\\resources\\public\\";
//            UPLOAD_DIR = Paths.get("src", "main", "resources", "public").toAbsolutePath().normalize().toString();
//             UPLOAD_DIR = "C:\\Users\\node\\Downloads\\shujutongji\\mardan-demo1\\src\\main\\resources\\public\\";
            UPLOAD_DIR = "D:\\files\\";
        } else {
            UPLOAD_DIR = "/home/oracle/uploadfiles";
        }
//      指定上传目录
        String uploadDirPath = UPLOAD_DIR;
//       确保上传目录存在
        File uploadDir = new File(uploadDirPath);
        if (!uploadDir.exists()) {
            boolean dirsCreate = uploadDir.mkdirs();
            if (!dirsCreate) {
                return Result.error("创建上传目录失败");
            }
        }
//        获取上传文件的原始文件名
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null || originalFilename.isEmpty()) {
            return Result.error("文件名不存在");
        }
//        生成随机数改造文件名称
        String randomName = UUID.randomUUID().toString();
//        构造保存文件的完整路径
        String filePath = Paths.get(uploadDirPath, randomName + originalFilename).toString();
        String returnFilePath = Paths.get(randomName + originalFilename).toString();
        try{
//            保存文件
            file.transferTo(new File(filePath));
            System.out.println("file " + filePath + "--------------------------------");
        }catch (IOException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
//        创建Filepojo 实体类
        FilePojo filePojo = new FilePojo();
        filePojo.setName(randomName + originalFilename);
        filePojo.setSize(String.valueOf(file.getSize()));
        filePojo.setPath(UPLOAD_DIR + randomName + originalFilename);
        filePojo.setType(file.getContentType());
        filePojo.setCreatedDate(LocalDateTime.now());
////        文件信息存储到数据库
        Boolean isSuccess = fileUploadService.fileUpload(filePojo);
        return Result.success("uploads/" +  returnFilePath );

    }
    /**
     * 无纸化会议pdf文件上传
     * ApiDes:
     * @param file
     * @return 响应描述：
     */

    @PostMapping("/wuzhihuahuiyiapi-uploadpdf")
    public Result wuZhiHuaHuiYiUploadPdf(MultipartFile file) throws IOException, URISyntaxException {
        String UPLOAD_DIR = "/home/oracle/uploadfiles";
        if (System.getProperty("os.name").toLowerCase().contains("windows")) {
            UPLOAD_DIR = "D:\\files\\";
        } else {
            UPLOAD_DIR = "/home/oracle/uploadfiles";
        }
//      指定上传目录
        String uploadDirPath = UPLOAD_DIR;
//       确保上传目录存在
        File uploadDir = new File(uploadDirPath);
        if (!uploadDir.exists()) {
            boolean dirsCreate = uploadDir.mkdirs();
            if (!dirsCreate) {
                return Result.error("创建上传目录失败");
            }
        }
//        获取上传文件的原始文件名
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null || originalFilename.isEmpty()) {
            return Result.error("文件名不存在");
        }
//        生成随机数改造文件名称
        String randomName = UUID.randomUUID().toString();
//        构造保存文件的完整路径
        String filePath = Paths.get(uploadDirPath, randomName + originalFilename).toString();
        String returnFilePath = Paths.get(randomName + originalFilename).toString();
        try{
//            保存文件
            file.transferTo(new File(filePath));
            System.out.println("file " + filePath + "--------------------------------");
        }catch (IOException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
//        创建Filepojo 实体类
        FilePojo filePojo = new FilePojo();
        filePojo.setName(originalFilename);
        filePojo.setSize(String.valueOf(file.getSize()));
        filePojo.setPath(UPLOAD_DIR + randomName + originalFilename);
        filePojo.setPath("uploads/" +  returnFilePath);
        filePojo.setType(file.getContentType());
        filePojo.setCreatedDate(LocalDateTime.now());
////        文件信息存储到数据库
        Boolean isSuccess = fileUploadService.fileUpload(filePojo);
        return Result.success(filePojo);

    }
    /**
     * 新增公告
     * ApiDes:
     * @param announcement
     * @return 响应描述：
     */
    @PostMapping("/gonggao-xinzeng")
    public Result gongGaoXinZeng(Announcement announcement) {
        return fileUploadService.gongGaoXinZeng(announcement);
    }
    /**
     * ApiName:通过id删除公告
     * ApiDes:
     * @param id 公告id
     * @return 响应描述：
     */
    @PostMapping("/gonggao-shanchu-byId")
    public Result gongGaoShanChuById(Integer id) {
        return fileUploadService.gongGaoShanChuById(id);
    }
    /**
     * ApiName:获取公告列表
     * ApiDes:
     * @param
     * @return 响应描述：
     */
    @PostMapping("/gonggao-getlist")
    public Result getGongGaoList(String title,
                                 Integer pageNum,
                                 Integer pageSize) {
        return fileUploadService.getGongGaoList(title, pageNum, pageSize);
    }
    /**
     * ApiName:根据公告id获取公告
     * ApiDes:
     * @param id
     * @return 响应描述：
     */
    @PostMapping("/gonggao-getbyid")
    public Result getGongGaoById(Integer id) {
        return fileUploadService.getGongGaoById(id);
    }
    /**
     * ApiName:根据id编辑公告
     * ApiDes:
     * @param announcement
     * @return 响应描述：
     */
    @PostMapping("/gonggao-updateById")
    public Result gongGaoUpdateById(Announcement announcement) {
        return fileUploadService.gongGaoUpdateById(announcement);
    }
    /**
     * ApiName:根据id设置隐藏或显示
     * ApiDes:
     * @param id
     * @return 响应描述：
     */
    @PostMapping("/gonggao-hiddenById")
    public Result gongGaoHiddenById(Integer id, Integer hidden) {
        return fileUploadService.gongGaoHiddenById(id, hidden);
    }
    /**
     * ApiName:用户获取公告列表，只能获取hidden为0的
     * ApiDes:
     * @param
     * @return 响应描述：
     */
    @PostMapping("/gonggao-list-foruser")
    public Result getUserGongGaoList(Integer pageNum,
                                   Integer pageSize,
                                   String title) {
        return fileUploadService.getUserGongGaoList(title, pageNum, pageSize);
    }


}
